package cn.usian.jyzx.product.service.impl;

import cn.usian.jyzx.model.dto.ProductItemVo;
import cn.usian.jyzx.model.dto.ProductSkuDto;
import cn.usian.jyzx.model.entity.product.Product;
import cn.usian.jyzx.model.entity.product.ProductDetails;
import cn.usian.jyzx.model.entity.product.ProductSku;
import cn.usian.jyzx.product.mapper.ProductDetailsMapper;
import cn.usian.jyzx.product.mapper.ProductMapper;
import cn.usian.jyzx.product.mapper.ProductSkuMapper;
import cn.usian.jyzx.product.service.ProductSkuService;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @TODO:
 * @Author:baimu
 * @Date:2024 10 30 14 28
 **/
@Service
@Slf4j
public class ProductSkuServiceImpl implements ProductSkuService {

    @Autowired
    ProductSkuMapper productSkuMapper;
    @Override
    public List<ProductSku> findBySaleNum() {
        return productSkuMapper.findBySaleNum();
    }

    @Override
    public PageInfo<ProductSku> findByPage(Integer page,
                                           Integer limit,
                                           ProductSkuDto productSkuDto) {
        PageHelper.startPage(page,limit);

        List<ProductSku> productSkus = productSkuMapper.findByPage(productSkuDto);


        return new PageInfo<>(productSkus);
    }


    @Autowired
    ProductMapper productMapper;

    @Autowired
    ProductDetailsMapper productDetailsMapper;

    @Override
    public ProductItemVo itemInfo(Long skuId) {
        ProductItemVo productItemVo = new ProductItemVo();
        //1:查询sku信息
        ProductSku productSku = productSkuMapper.findById(skuId);
        log.info("productSku:{}",productSku.toString());
        productItemVo.setProductSku(productSku);

        //2:查询商品信息
        Long productId = productSku.getProductId();
        Product product = productMapper.findById(productId);
        log.info("product:{}",product.toString());
        productItemVo.setProduct(product);

        /**
         * 3:查询商品轮播图
         * http://139.198.127.41:9000/spzx/20230525/665832167-5_u_1.jpg,http://139.198.127.41:9000/spzx/20230525/665832167-6_u_1.jpg,http://139.198.127.41:9000/spzx/20230525/665832167-4_u_1.jpg,http://139.198.127.41:9000/spzx/20230525/665832167-1_u_1.jpg,http://139.198.127.41:9000/spzx/20230525/665832167-5_u_1 (1).jpg,http://139.198.127.41:9000/spzx/20230525/665832167-3_u_1.jpg
         */
        String sliderUrls = product.getSliderUrls();
        String[] sliderUrlsString = sliderUrls.split(",");
        List<String> stringList = Arrays.asList(sliderUrlsString);
        log.info("sliderUrlsString:{}",stringList);
        productItemVo.setSliderUrlList(stringList);


        /**
         * 4:商品详情表 product_details
         */
        ProductDetails productDetails = productDetailsMapper.findByProductId(productId);
        String imageUrls = productDetails.getImageUrls();
        String[] imageUrlsString = imageUrls.split(",");
        List<String> imageUrlList = Arrays.asList(imageUrlsString);
        log.info("imageUrlList:{}",imageUrlList);
        productItemVo.setDetailsImageUrlList(imageUrlList);


        //5:sku的规格对应skuId
        Map<String , Object> skuSpecValueMap = new HashMap<String , Object>();
        //key，value；
        List<ProductSku> productSkuList = productSkuMapper.findByProductId(productId);
        productSkuList.forEach(item->{
            skuSpecValueMap.put(item.getSkuSpec(),item.getId());
        });
        log.info("skuSpecValueMap:{}",skuSpecValueMap);
        productItemVo.setSkuSpecValueMap(skuSpecValueMap);



        //6：商品规格 -- product表中的 spec_value
        //[{"key":"颜色","valueList":["白色11","红色","黑色22"]},{"key":"内存","valueList":["8G","18G"]}]
        String specValue = product.getSpecValue();
//        //json 字符串转化成集合
//        JSONArray jsonArray = JSONArray.parseArray(specValue);
//
//        List<Map<String,Object>> mapList = new ArrayList<Map<String,Object>>();
//        jsonArray.forEach(item->{
//
//
//        });

        JSONArray jsonArray = JSONArray.parseArray(specValue);
        productItemVo.setSpecValueList(jsonArray);
        return productItemVo;
    }

    @Override
    public ProductSku findById(Long skuId) {
        return productSkuMapper.findById(skuId);
    }
}
